package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckgroupMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(interfaceClass = CheckGroupService.class)
public class CheckGroupServiceImpl implements CheckGroupService{

    @Autowired
    private CheckgroupMapper checkgroupMapper;

    @Override
    public PageResult findList(QueryPageBean queryPageBean) {

        //传入分页参数thread local记录分页信息
        Page<CheckGroup> pageInfo = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize())
                .doSelectPage(() -> checkgroupMapper.findList(queryPageBean.getQueryString()));

        //取出thread local中的信息
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getResult());

        return pageResult;
    }

    @Override
    public Result update(CheckGroup checkGroup) {
        final Boolean update = checkgroupMapper.update(checkGroup);
        if(!update){
            return  new Result(update,MessageConstant.EDIT_CHECKGROUP_FAIL,null);
        }

        final CheckGroup data = checkgroupMapper.findById(checkGroup.getId());

        data.setName(checkGroup.getName());
        data.setSex(checkGroup.getSex());
        data.setHelpCode(checkGroup.getHelpCode());
        data.setRemark(checkGroup.getRemark());
        data.setAttention(checkGroup.getAttention());
        data.setCheckItems(checkGroup.getCheckItems());

        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS,data);
    }

    @Override
    public Result findById(Integer id) {
        final CheckGroup checkGroup = checkgroupMapper.findById(id);

        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
    }

    @Override
    public List<CheckItem> findCheckItemIdsByCheckGroupId(Integer id) {

        return checkgroupMapper.findCheckItemIdsByCheckGroupId(id);
    }


}
